package com.hboxs.asl.controller.asl.member;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.Trade;
import com.hboxs.asl.service.MemberService;
import com.hboxs.asl.service.TradeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;

/**
 * Created by xxx on 2016/12/21
 */
@Controller(value = "aslWalletFavorController")
@RequestMapping(value = "/weChat/wallet")
public class WalletControl extends BaseController {

    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "tradeServiceImpl")
    private TradeService tradeService;


    /**
     * 充值记录列表页面，默认首页
     *
     * @param modelMap
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/recharging", method = RequestMethod.GET)
    public String getRechargingRecord(ModelMap modelMap, Pageable pageable) {
        Member member = memberService.getCurrent();
        modelMap.addAttribute("balance", member.getBalance());
        modelMap.addAttribute("page", tradeService.findPage(pageable, member, Trade.Style.pay));
        modelMap.addAttribute("style", Trade.Style.pay);
        return "/yijing/wallet";
    }

    /**
     * 消费列表页面
     *
     * @param modelMap
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/consumption", method = RequestMethod.GET)
    public String getConsumptionRecord(ModelMap modelMap, Pageable pageable) {
        Member member = memberService.getCurrent();
        modelMap.addAttribute("balance", member.getBalance());
        modelMap.addAttribute("page", tradeService.findPage(pageable, member, Trade.Style.waste));
        modelMap.addAttribute("style", Trade.Style.waste);
        return "/yijing/wallet";
    }


    /**
     * 我的钱包分页信息
     *
     * @param pageable
     * @param style
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public
    @ResponseBody
    Object index(Pageable pageable, Trade.Style style) {
        try {
            Member member = memberService.getCurrent();
            if (style == null) {
                return JsonMessage.error("记录类型不能为空");
            }
            Page page = tradeService.findPage(pageable, member, style);
            return JsonMessage.success(
                    "page", getPageInfo(page),
                    "balance", member.getBalance().setScale(2, BigDecimal.ROUND_HALF_UP),
                    "trades", JsonMessage.optimiseObj(page.getContent(), "createDate", "amount", "completeDate")
            );

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 获取 page 对象中非 content 的部分信息
     *
     * @param page
     * @return
     */
    private HashMap<String, Integer> getPageInfo(Page page) {
        HashMap<String, Integer> pageInfo = new HashMap<>();
        pageInfo.put("totalPages", page.getTotalPages());
        pageInfo.put("pageNumber", page.getPageNumber());
        pageInfo.put("pageSize", page.getPageSize());
        return pageInfo;
    }


}
